import { Database } from '@hw-agconnect/cloud/src/main/ets/database/Database';
import { DeliveryOrder } from './model/DeliveryOrder';

const TAG = "DeliveryOrderOperation"

export class DeliveryOrderOperation {
  public static upsert(order: DeliveryOrder) {
    try {
      const record = AppStorage.Get<Database>('agcDataBase').collection(DeliveryOrder).upsert(order);
      console.info(TAG, "upsert success, record  = " + JSON.stringify(record));
    } catch (err) {
      console.error(TAG, "upsert err------------" + JSON.stringify(err));
    }
  }

  public static querySendOrder(userId: string): Promise<DeliveryOrder[]> {
    return new Promise(async (resolve: Function) => {
      const resultArray = AppStorage.Get<Database>('agcDataBase').collection(DeliveryOrder)
        .query().equalTo('userId', userId).get();
      console.info(TAG, "query success : " + JSON.stringify(resultArray));
      resolve(resultArray)
    })
  }

  public static queryRecvOder(recverPhone: string): Promise<DeliveryOrder[]> {
    return new Promise(async (resolve: Function) => {
      const resultArray = AppStorage.Get<Database>('agcDataBase').collection(DeliveryOrder)
        .query().equalTo('recverPhone', recverPhone).get();
      console.info(TAG, "query success : " + JSON.stringify(resultArray));
      resolve(resultArray)
    })
  }


}

